package TUILayer;
import java.util.Scanner;
import ControllerLayer.*;

/**
 * Write a description of class PartOrderUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PartOrderUI
{
    // instance variables - replace the example below with your own
    private OrderController oCtr;
    private ProductController pCtr;

    /**
     * Constructor for objects of class PartOrderUI
     */
    public PartOrderUI()
    {
        // initialise instance variables
        oCtr = OrderController.getInstance();
        pCtr = ProductController.getInstance();
    }

    public void start()
    {
        Menu();
    }

    public void Menu()
    {
        boolean exit = false;

        while(!exit)  //if exit = true, the main menu will shut down 
        {
            int choice = writeMainMenu();
            switch(choice)
            {
                case 1:
                create();
                break;
                case 2:
                read();
                break;
                case 3:
                update();
                break;                
                case 4:
                delete();
                break;
                case 5:
                printID();
                break;
                case 6:
                printAll();
                break;
                default:                
                exit = true;
                break; 
            }
        }
    }

    public int writeMainMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f ****Order Menu***");
        System.out.println("  (1) Create");
        System.out.println("  (2) Read");
        System.out.println("  (3) Update");
        System.out.println("  (4) Delete");
        System.out.println("  (5) Print ID");
        System.out.println("  (6) Print ALL");
        System.out.println("  (7) Return");
        System.out.println("\n make your choice: ");

        int choice = keyboard.nextInt();

        return choice;

    }

    private long inputIDOrder()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);        
        System.out.println("Type Order id: " );
        long out = keyboard.nextLong();
        return out;
    }

    private long inputIDPartOrder()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);        
        System.out.println("Type Part Order id: " );
        long out = keyboard.nextLong();
        return out;
    }

    private long inputIDProductDescription()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);        
        System.out.println("Type Product Description id:  " );
        long out = keyboard.nextLong();
        return out;
    }

    private long inputIDProductNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);        
        System.out.println("Type Product No id: " );
        long out = keyboard.nextLong();
        return out;
    }

    private boolean inputDone()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type if active or not (y/n): " );
        String answer = keyboard.nextLine();
        if(answer == "y")
        {
            return true;
        }
        else 
        {
            return false;
        }
    } 

    private boolean inputYesOrNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Do you wanna update (y/n): " );
        String answer = keyboard.nextLine();
        if(answer == "y")
        {
            return true;
        }
        else 
        {
            return false;
        }
    } 

    public void create()
    {
        Scanner keyboard = new Scanner(System.in);

        long orderID = inputIDOrder();

        if(orderID > 0 && oCtr.readOrder(orderID) != null)
        {
            long pd = inputIDProductDescription();            
            if(pd > 0 && pCtr.readProduct(pd) != null)
            {
                long pn = inputIDProductNo();
                if(pn > 0 && pCtr.readProductNo(pd, pn) != null)
                {
                    oCtr.createPartOrder(orderID, pd, pn);
                    long id = oCtr.getID();
                    System.out.println("      new Part Order has been created - se info under");
                    oCtr.printOnePartOrderID(orderID, id);
                    System.out.println(" Part Order code nr.: "+oCtr.readPartOrder(orderID, id)); 
                }
                else
                {
                    System.out.println("      Product no was not found - try another ID");
                }
            }
            else
            {
                System.out.println("      Product Description not found - try another ID");
            }
        }
        else
        {
            System.out.println("    Part Order is not created - try another Order ID");
            System.out.println("    Required an order ID");
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void read()
    {    
        Scanner keyboard = new Scanner(System.in);

        long orderID = inputIDOrder();
        if(orderID != 0 && oCtr.readOrder(orderID) != null)
        {
            long partID = inputIDPartOrder();
            if(partID != 0 && oCtr.readPartOrder(orderID, partID) != null)
            {
                System.out.println("       Part Order was found - se info under");
                oCtr.printOnePartOrderID(orderID, partID);
                System.out.println("  Part Order code nr.: "+oCtr.readPartOrder(orderID, partID));
            }
            else
            {
                System.out.println("    Part Order was not found - try another ID");
            }
        }
        else
        {
            System.out.println("    Order was not found - try another ID");            
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void update()
    {
        Scanner keyboard = new Scanner(System.in);

        long orderID = inputIDOrder();
        if(orderID > 0 && oCtr.readOrder(orderID) != null)
        {
            long partID = inputIDPartOrder();
            if(partID > 0 && oCtr.readPartOrder(orderID, partID) != null)
            {
                long pd = inputIDProductDescription();
                if(pCtr.readProduct(pd) != null)
                {
                    long pn = inputIDProductNo();
                    if(pCtr.readProductNo(pd, pn) != null)
                    {
                        oCtr.updatePartOrder(orderID, partID, pd, pn);
                        System.out.println("      Part Order has been updated - se info under: ");
                        oCtr.printOnePartOrderID(orderID, partID);
                        System.out.println(" Part Order code nr.: "+oCtr.readPartOrder(orderID, partID));
                    }
                    else
                    {
                        System.out.println("      Product no was not found - try another ID");
                    }
                }
                else
                {
                    System.out.println("      Product Description not found - try another ID");
                }                
            }
            else
            {
                System.out.println("      Part Order was not found - try another ID");
            }
        }
        else
        {
            System.out.println("      Order was not found - try another ID");
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void delete()
    {
        Scanner keyboard = new Scanner(System.in);

        long orderID = inputIDOrder();
        if(orderID != 0 && oCtr.readOrder(orderID) != null)
        {
            long partID = inputIDPartOrder();
            if(partID != 0 && oCtr.readPartOrder(orderID, partID) != null)
            {
                System.out.println("       Part Order is deleted - se info under");
                oCtr.printOnePartOrderID(orderID, partID);
                System.out.println("  Part Order code nr.: "+oCtr.readPartOrder(orderID, partID));
                oCtr.deletePartOrder(orderID, partID);
            }
            else
            {
                System.out.println("    Part Order was not found - try another ID");
            }
        }
        else
        {
            System.out.println("    Order was not found - try another ID");            
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void printID()
    {
        Scanner keyboard = new Scanner(System.in);

        long orderID = inputIDOrder();
        if(orderID != 0 && oCtr.readOrder(orderID) != null)
        {
            long partID = inputIDPartOrder();
            if(partID != 0 && oCtr.readPartOrder(orderID, partID) != null)
            {
                System.out.println("       Part Order - se info under");
                oCtr.printOnePartOrderID(orderID, partID);
                System.out.println("  Part Order code nr.: "+oCtr.readPartOrder(orderID, partID));                
            }
            else
            {
                System.out.println("    Part Order was not found - try another ID");
            }
        }
        else
        {
            System.out.println("    Order was not found - try another ID");            
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void printAll()
    {
        Scanner keyboard = new Scanner(System.in);

        long orderID = inputIDOrder();
        if(orderID != 0 && oCtr.readOrder(orderID) != null)
        {
            oCtr.printAllPartOrder();
        }
        else
        {
            System.out.println("    Order was not found - try another ID");            
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }
}
